package com.sxm.indoormanagement.receive;

import com.google.gson.Gson;
import com.sxm.indoormanagement.entity.Atmosphere;
import com.sxm.indoormanagement.utils.JudgeDataUtil;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * @author 780013490@qq.com  孙小明
 * @version 1.0
 * @date 2020/6/13 17:37
 * @description 把接受的上位机程序解析后用http发送给服务器
 */


public class SendData {

    private static final MediaType MEDIA_TYPET =
            MediaType.parse("application/json; charset=utf-8");
    private static final String URL_HEAD = "http://localhost:18081/";

    private final String url;
    private final OkHttpClient okHttpClient;
    private final Gson gson;


    public SendData() {
        okHttpClient = new OkHttpClient();
        url = URL_HEAD + "receivedata";
        gson = new Gson();
    }

    /**
     * @param s 最原始的字符串数据
     */
    public void send(String s) {
        String body = gson.toJson(JudgeDataUtil.getData(s));
        RequestBody requestBody = RequestBody.Companion.create(body, MEDIA_TYPET);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {


            }

            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }
        });

    }
}
